import { defineStore } from 'pinia'
import type { ICart, IOderNum } from '@/types/types';

export const usePayOderStore = defineStore('payOder', {
    state() {
        return {
            checkGoodsList: [] as Array<ICart>,
            oderList: [] as Array<IOderNum>,
            goPayList: [] as Array<string>,
        }
    },
    actions: {
        // 保存单选中的商品
        saveCheckGoodsList(data: any) {
            this.checkGoodsList = data
        },
        saveOderList(data: any) {
            this.oderList = data
        },
        // 保存订单号
        saveGoPayList(data: any) {
            this.goPayList = data
        },
    },
    getters: {
        // 总价格
        totalPrice(): number {
            const sum = this.checkGoodsList.reduce(
                // preventvalue 先前值  currentvalue 当前值
                (preventvalue, currentvalue) =>
                    preventvalue + currentvalue.price * currentvalue.num,
                0
            )
            return Number(sum.toFixed(2)) * 100
        },
        getCheckGoodsList(): any {
            return this.checkGoodsList
        },
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'payOder', //设置存储的key
                storage: localStorage, //表示存储在localStorage
            },
        ],
    },
})
